﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

/**
 * 多播委托
 */
namespace P19_5 {
    internal class Program {
        static void Main(string[] args) {

            Student stu1 = new Student() { ID=1,PenColor = ConsoleColor.Yellow};
            Student stu2 = new Student() { ID=2,PenColor = ConsoleColor.Green};
            Student stu3 = new Student() { ID=3,PenColor = ConsoleColor.Red};
            Action action1 = new Action(stu1.DoHomework);
            Action action2 = new Action(stu2.DoHomework);
            Action action3 = new Action(stu3.DoHomework);

            //action1.Invoke();
            //action2.Invoke();
            //action3.Invoke();

            //多播委托(间接同步调用)
            action1 += action2+=action3;
             //或者这样
            //action1 += action3;
            //执行的时候是按照顺序执行的，1-->2-->3
            action1.Invoke();
            Console.WriteLine("========================");
            //同步调用
            stu1.DoHomework();
            stu2.DoHomework();
            stu3.DoHomework();
            Console.WriteLine("========================");
            //间接同步调用
            action1.Invoke();
            action2.Invoke();
            action3.Invoke();
            Console.WriteLine("======================");


            Student stu4 = new Student() { ID = 4, PenColor = ConsoleColor.Yellow };
            Student stu5 = new Student() { ID = 5, PenColor = ConsoleColor.Green };
            Student stu6 = new Student() { ID = 6, PenColor = ConsoleColor.Red };
            Action action4 = new Action(stu4.DoHomework);
            Action action5 = new Action(stu5.DoHomework);
            Action action6 = new Action(stu6.DoHomework);
            //异步调用
            action4.BeginInvoke(null, null);
            action5.BeginInvoke(null, null);
            action6.BeginInvoke(null, null);
            Thread.Sleep(1000);

            Console.WriteLine("======================");
            Student stu7 = new Student() { ID = 7, PenColor = ConsoleColor.Yellow };
            Student stu8 = new Student() { ID = 8, PenColor = ConsoleColor.Green };
            Student stu9 = new Student() { ID = 9, PenColor = ConsoleColor.Red };
            Thread thread1 = new Thread(new ThreadStart(stu7.DoHomework));
            Thread thread2 = new Thread(new ThreadStart(stu8.DoHomework));
            Thread thread3 = new Thread(new ThreadStart(stu9.DoHomework));
            //异步调用
            thread1.Start();
            thread2.Start();
            thread3.Start();
            Thread.Sleep(1000);
            Console.WriteLine("======================");

            Student stu10 = new Student() { ID = 10, PenColor = ConsoleColor.Yellow };
            Student stu11 = new Student() { ID = 11, PenColor = ConsoleColor.Green };
            Student stu12 = new Student() { ID = 12, PenColor = ConsoleColor.Red };
            //异步调用
            Task task1 = new Task(new Action(stu10.DoHomework));
            Task task2 = new Task(new Action(stu11.DoHomework));
            Task task3 = new Task(new Action(stu12.DoHomework));
            task1.Start();
            task2.Start();
            task3.Start();
            Console.ReadLine();
        } 
    }

    class Student {
        public int ID { get; set; }
        public ConsoleColor PenColor { get; set; }

        public void DoHomework() {
            for (int i = 0; i < 5; i++)
            {
                Console.ForegroundColor = this.PenColor;
                Console.WriteLine("Student {0} doing homework {1} hour(s)",this.ID,i);
                //Thread.Sleep(1000);
            }
        }
    }
}
